package com.ruoyi.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.marketing.domain.Activity;
import com.ruoyi.marketing.domain.ActivityUser;
import com.ruoyi.marketing.domain.MarketDetail;
import com.ruoyi.marketing.domain.MarketingPrize;
import com.ruoyi.marketing.mapper.MarketDetailMapper;
import com.ruoyi.marketing.service.IActivityService;
import com.ruoyi.marketing.service.IActivityUserService;
import com.ruoyi.marketing.service.IMarketDetailService;
import com.ruoyi.marketing.service.IMarketingPrizeService;
import com.ruoyi.marketing.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: renSen
 * @Date: 2023/01/30
 * @Description:
 */
@Service
public class MarketDetailServiceImpl extends ServiceImpl<MarketDetailMapper, MarketDetail> implements IMarketDetailService {
    @Autowired
    private IActivityUserService activityUserService;
    @Autowired
    private IMarketingPrizeService marketingPrizeService;
    @Autowired
    private IActivityService activityService;

    @Override
    public void draw(Long activityId) {
        //获取当前登录用户id
        Long userId = SecurityUtils.getUserId();
        //查看当前用户是否有抽奖次数
        ActivityUser activityUser = activityUserService.getOne(new LambdaQueryWrapper<ActivityUser>()
                .eq(ActivityUser::getActivityId, activityId)
                .eq(ActivityUser::getUserId, userId));
        //有抽奖次数
        if (activityUser.getSurplus() > 0) {
            //获取当前活动下的奖品
           Activity activity = activityService.getById(activityId);
            List<MarketingPrize> prizes = marketingPrizeService.list(new LambdaQueryWrapper<MarketingPrize>().eq(MarketingPrize::getType, activity.getPrizeType()));
            //获取所有中奖的权重
            List<Double> doubles = prizes.stream().map(l -> l.getProbability()).collect(Collectors.toList());
            //调用工具类开始抽奖
           MarketingPrize prize = prizes.get(Util.lottery(doubles));

            //抽奖次数-1
            activityUser.setSurplus(activityUser.getSurplus() - 1);
            //判断是否抽中奖品
            if (!"谢谢回顾".equals(prize.getName())) {
                //记录抽奖记录
                MarketDetail marketDetail = new MarketDetail();
                marketDetail.setActivityId(activityId);
                marketDetail.setLotteryTime(new Date());
                marketDetail.setRewardId(prize.getId());
                marketDetail.setUserId(userId);
                //默认现场领取
                marketDetail.setMode(3);
                super.save(marketDetail);
                //中奖次数+1
                activityUser.setPrize(activityUser.getPrize() + 1);
            }
            activityUserService.updateById(activityUser);
            return;
        }
        throw new ServiceException("没有抽奖次数");
    }
}
